package feishuUtil

import (
	"encoding/json"
	"fmt"
	"go-api-template/service"
	"strings"
)

// MessageI 数据文档接口
type MessageI interface {
	SendText(receiveIdType string, receiveId string, msgType string, content string) error
}

type ContentPost struct {
	Post struct {
		ZhCn struct {
			Title   string             `json:"title"`
			Content [][]MessageContent `json:"content"`
		} `json:"zh_cn"`
	} `json:"post"`
}

type MessageContent struct {
	Tag      string `json:"tag"`
	Text     string `json:"text,omitempty"`
	Href     string `json:"href,omitempty"`
	UserID   string `json:"user_id,omitempty"`
	UserName string `json:"user_name,omitempty"`
}

type ContentText struct {
	Text string `json:"text"`
}

type ContentImage struct {
	ImageKey string `json:"image_key"`
}

func (fc FeishuClient) SendText(receiveIdType string, receiveId string, msgType string, content string) (err error) {

	bodySt := struct {
		ReceiveId string `json:"receive_id"`
		Content   string `json:"content"`
		MsgType   string `json:"msg_type"`
	}{
		ReceiveId: receiveId,
		Content:   content,
		MsgType:   msgType,
	}
	bodyByte, err := json.Marshal(bodySt)
	if err != nil {
		return
	}

	fg, err := fc.request("POST", fmt.Sprintf("/open-apis/im/v1/messages?receive_id_type=%s", receiveIdType), strings.NewReader(string(bodyByte)))
	if err != nil {
		return
	}
	service.Log.Debug(string(fg))

	return
}
